package com.syt.hospital.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.syt.hospital.service.HospitalSetService;
import com.syt.model.hospital.HospitalSet;
import com.syt.result.Result;
import com.syt.vo.hospital.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 医院设置表 前端控制器
 * </p>
 *
 * @author xxx
 * @since 2023-02-01
 */
@Api(tags = "医院设置表管理字典")
@RestController
@RequestMapping("/admin/hospital/hospitalSet")
//@CrossOrigin 有 gateway 做了跨域
public class HospitalSetController {

    @Autowired
    private HospitalSetService hospitalSetService;

    @ApiOperation(value = "发送签名密钥")
    @PutMapping("/sendKey/{id}")
    public Result lockHospitalSet(@PathVariable("id") long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        //密钥
        String signKey = hospitalSet.getSignKey();
        //医院编号
        String hoscode = hospitalSet.getHoscode();
        //TODO 发送信息
        return Result.ok();


    }


    @ApiOperation(value = "修改医院设置锁定或解锁")
    @PutMapping("/lockHospitalSet/{id}/{status}")
    public Result lockHospitalSet(@PathVariable("id") long id,
                                  @PathVariable("status") int status) {
        boolean lock = hospitalSetService.lockHospitalSet(id, status);
        if (lock) {
            return Result.ok();
        } else {
            return Result.fail();
        }

    }


    @ApiOperation(value = "批量删除医院设置")
    @DeleteMapping("/batchRemoveHospitalSet")
    public Result batchRemoveHospitalSet(@RequestBody List<Long> idList) {
        boolean batchRemove = hospitalSetService.batchRemoveHospitalSet(idList);
        if (batchRemove) {
            return Result.ok();
        } else {
            return Result.fail();
        }

    }

    @ApiOperation(value = "根据id修改医院设置")
    @PostMapping("/updateHospitalSetById")
    public Result updateHospitalSetById(@RequestBody HospitalSet hospitalSet) {

        boolean update = hospitalSetService.updateHospitalSetById(hospitalSet);
        if (update) {
            return Result.ok(update);
        } else {
            return Result.fail(update);
        }
    }


    @ApiOperation(value = "根据id获取医院设置")
    @GetMapping("/getHospitalSet/{id}")
    public Result getHospitalSetById(@PathVariable("id") Long id) {
        return Result.ok(hospitalSetService.getById(id));
    }

    @ApiOperation(value = "添加医院设置")
    @PostMapping("/saveHospitalSet")
    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet) {

        boolean save = hospitalSetService.saveHospitalSet(hospitalSet);
        if (save) {
            return Result.ok();
        }
        return Result.fail();
    }


    @ApiOperation(value = "条件查询加分页")
    @PostMapping("/findPageHospitalSet/{current}/{size}")
    public Result findPageHospitalSet(@PathVariable("current") long current,
                                      @PathVariable("size") long size,
                                      @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo) {
        //创建page对象，传递当前页，每页的记录数
        Page<HospitalSet> page = new Page<>(current, size);
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
        //构架条件查询
        if (!StringUtils.isEmpty(hospitalSetQueryVo.getHosname())) {
            wrapper.like("hosname", hospitalSetQueryVo.getHosname());
        }
        if (!StringUtils.isEmpty(hospitalSetQueryVo.getHoscode())) {
            wrapper.eq("hoscode", hospitalSetQueryVo.getHoscode());
        }

        //调用方法实现分页查询
        Page<HospitalSet> hospitalSetPage = hospitalSetService.page(page, wrapper);
        return Result.ok(hospitalSetPage);
    }


    @ApiOperation(value = "根据医院Id进行逻辑删除")
    @DeleteMapping("/{id}")
    public Result removeHospitalById(@PathVariable("id") Long id) {
        boolean falg = hospitalSetService.removeHospitalById(id);
        if (falg) {
            return Result.ok();
        }
        return Result.fail();
    }

    @ApiOperation(value = "查询医院设置表所有信息")
    @GetMapping("/findAllHospitalSet")
    public Result findAllHospitalSet() {
        List<HospitalSet> list = hospitalSetService.list();
        return Result.ok(list);
    }


}
